home *** CD-ROM | disk | FTP | other *** search
/ CD Fun House 8 / CD Funhouse Version 8.0 - Wayzata Technology (7013) (1993).iso / pc / pc / cards / bidding / deal.c < prev    next >
C/C++ Source or Header  |  1989-02-14  |  2KB  |  97 lines

  1. /* deal.c */
  2. /*
  3.             Bridge Bidder    Version 2.0
  4.             by Nathan Glasser
  5.             nathan@brokaw.lcs.mit.edu (internet)
  6.             nathan@mit-eddie.uucp (usenet)
  7.  
  8.             February, 1989
  9. ------------------------------------------------------------------------------
  10. Copyright 1988, 1989 by Nathan Glasser.
  11. You may feel free to distribute this program in its current form.
  12. Please do not remove this copyright information.
  13. */
  14.  
  15. #include "bidding.h"
  16.  
  17.  
  18. card_compare(pcard1,pcard2)
  19. card *pcard1,*pcard2;
  20. {
  21.     return((pcard1->suit != pcard2->suit) ? (pcard1->suit - pcard2->suit) :
  22.        (pcard2->rank - pcard1->rank));
  23. }
  24.  
  25. deal_hands(pdeal)
  26. deal *pdeal;
  27. {
  28.     card *thehand;
  29.     int cards[52];
  30.     int player,cardsinhand;
  31.     int cardnum;
  32.     int nextcard;
  33.     int cardsleft = 52;
  34.     static vulnerability = 0;
  35.  
  36.     pdeal->num_bids = 0;
  37.     pdeal->bidding_done = 0;
  38.     vulnerability = ((pdeal->vulnerability = vulnerability) + 1) % 4;
  39.     pdeal->opening_lead.rank = pdeal->opening_lead.suit = -1;
  40.  
  41.     for (cardnum = 0; cardnum < 52; cardnum++)
  42.     cards[cardnum] = 0;
  43.  
  44.     for (player = 0; player < 4; player++)
  45.     {
  46.     thehand = pdeal->hands[player];
  47.     for (cardsinhand = 0; cardsinhand < 13; cardsinhand++)
  48.     {
  49.         nextcard = random() % (cardsleft--);
  50.         for (cardnum = 0 ; cards[cardnum] || --nextcard >= 0; cardnum++);
  51.         cards[cardnum] = 1;
  52.         thehand[cardsinhand].suit = cardnum / 13;
  53.         thehand[cardsinhand].rank = 2 + (cardnum % 13);
  54.     }
  55.     qsort(thehand,13,sizeof(card),card_compare);
  56.     }
  57. }
  58.  
  59.  
  60. print_hand(fp,thehand)
  61. FILE *fp;
  62. card *thehand;
  63. {
  64.     int i;
  65.     char bufs[4][80];
  66.  
  67.     format_hand(bufs,thehand);
  68.     for (i = 0; i < 4; i++)
  69.     {
  70.         fputs(bufs[i],fp);
  71.     putc('\n',fp);
  72.     }
  73. }
  74.  
  75. /* Accepts an array of size four of strings, and formats into it */
  76. format_hand(bufs,thehand)
  77. char (*bufs)[80];
  78. card *thehand;
  79. {
  80.     extern char *suit_strings[];
  81.     static char card_chars[] = "xx23456789TJQKA";
  82.     int suit_num = -1;
  83.     int i;
  84.     static char card_str[3] = " A";
  85.  
  86.     bufs--;
  87.     for (i = 0; i < 13; i++)
  88.     {
  89.     while (thehand[i].suit != suit_num)
  90.         sprintf(*++bufs,"%8s:  ",suit_strings[++suit_num]);
  91.     card_str[1] = card_chars[thehand[i].rank];
  92.     strcat(*bufs,card_str);
  93.     }
  94.     while (suit_num < 3)
  95.         sprintf(*++bufs,"%8s:  ",suit_strings[++suit_num]);
  96. }
  97.